/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package cn.s1995.sloth.bms.controller;

import cn.s1995.sloth.bizs.entity.RegionData;
import cn.s1995.sloth.bizs.oo.vo.PCDTreeVO;
import cn.s1995.sloth.bizs.service.RegionDataService;
import cn.s1995.sloth.common.core.util.R;
import cn.s1995.sloth.common.log.annotation.SysLog;
import cn.s1995.sloth.common.security.annotation.Inner;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;


/**
 * 地区表
 *
 * @author penf
 * @date 2022-03-01 15:58:59
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/region-data")
@Tag(description = "RegionData", name = "BMS-地区表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RegionDataController {

    private final RegionDataService regionDataService;

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @param req  地区表
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('admin_region_data_get')")
    public R getPage(@ParameterObject Page page, @ParameterObject RegionData req) {
        return R.ok(
                regionDataService.page(
                        page,
                        Wrappers.lambdaQuery(RegionData.class)
                                .and(StringUtils.isNotEmpty(req.getSearch()),
                                        wrapper -> wrapper
                                                .like(RegionData::getName, req.getSearch())
                                                .or()
                                                .like(RegionData::getShortName, req.getSearch())
                                                .or()
                                                .like(RegionData::getZipCode, req.getSearch())
                                                .or()
                                                .like(RegionData::getPinyin, req.getSearch())
                                                .or()
                                                .like(RegionData::getMergerName, req.getSearch()))
                ));
    }

    /**
     * 通过id查询地区表
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('admin_region_data_get')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(regionDataService.getById(id));
    }

    /**
     * 新增地区表
     *
     * @param regionData 地区表
     * @return R
     */
    @Operation(summary = "新增地区表", description = "新增地区表")
    @SysLog("新增地区表")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('admin_region_data_add')")
    public R save(@RequestBody RegionData regionData) {
        return R.ok(regionDataService.save(regionData));
    }

    /**
     * 修改地区表
     *
     * @param regionData 地区表
     * @return R
     */
    @Operation(summary = "修改地区表", description = "修改地区表")
    @SysLog("修改地区表")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin_region_data_edit')")
    public R updateById(@RequestBody RegionData regionData) {
        return R.ok(regionDataService.updateById(regionData));
    }

    /**
     * 通过id删除地区表
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id删除地区表", description = "通过id删除地区表")
    @SysLog("通过id删除地区表")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('admin_region_data_del')")
    public R removeById(@PathVariable Long id) {
        return R.ok(regionDataService.removeById(id));
    }


    @Inner(value = false)
    @ApiOperationSupport(order = 3)
    @Operation(summary = "获取地区数据树", description = "")
    @Parameters({
            @Parameter(name = "levelList", description = "所需层级，默认返回所有数据即省市区三级，若只需要部分数据可使用此参数，比如只获取省市数据 eg: [1,2]", example = "range[1, 10]"),
    })
    @GetMapping("/tree")
    public R<List<PCDTreeVO>> getRegionData(@RequestParam(required = false) Set<Integer> levelList) {
        return R.ok(regionDataService.getTree(levelList));
    }

}
